package com.pzhu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pzhu.model.entity.Order;
import com.pzhu.model.entity.User;
import com.pzhu.model.entity.Wage;
import com.pzhu.model.help.ResponseResult;
import com.pzhu.model.vo.WageVo;
import com.pzhu.service.*;
import com.pzhu.utils.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author : zqh
 * @Date : 2023/12/29 0029 下午 4:46
 * @Version: 1.0
 */
@Api(tags = "统计图信息")
@RestController
@RequestMapping("/chart")
public class ChartController {

    @Autowired
    private WageService wageService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private StaffService staffService;

    @Autowired
    private UserService userService;

    @Autowired
    private ThaliService thaliService;


    @ApiOperation("查询工资信息")
    @GetMapping("/wage")
    public ResponseResult wageList(){

        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);

        //工资
        List<Wage> wageList = wageService.list(
                new QueryWrapper<Wage>()
                        .select("MONTH(create_time) AS month" ,"SUM(wage) as sum")
                        .apply("date_format(create_time,'%Y') = "+year)
                        .groupBy("month"));

        List<WageVo> wageChart = BeanCopyUtils.copyBeanList(wageList, WageVo.class);


        //总收益
        List<Order> orderList = orderService.list(
                new QueryWrapper<Order>()
                        .select("MONTH(update_time) AS month", "SUM(total_amount) as sum")
                        .apply("date_format(update_time,'%Y') = " + year)
                        .eq("status", 2)
                            .groupBy("month")
        );

        List<WageVo> orderChart = BeanCopyUtils.copyBeanList(orderList, WageVo.class);

        orderChart.forEach(v -> v.setMonth(v.getMonth() - 1));
        wageChart.forEach(v -> v.setMonth(v.getMonth() - 1));

        return ResponseResult.ok().put("wage",wageChart).put("order",orderChart);
    }

    @ApiOperation("查询数量")
    @GetMapping("/count")
    public ResponseResult count(){
        int orderCount = orderService.count(new LambdaQueryWrapper<Order>().eq(Order::getStatus, 2));
        int userCount = userService.count(new LambdaQueryWrapper<User>().eq(User::getRole, 2));
        int staffCount = staffService.count();

        Order order = orderService.getOne(new QueryWrapper<Order>().select("sum(total_amount) as sum").eq("status", 2));

        Map<String,Object> map = new HashMap<>();
        map.put("orderCount",orderCount);
        map.put("userCount",userCount);
        map.put("staffCount",staffCount);
        map.put("money",order.getSum());

        return ResponseResult.ok(map);
    }

    @ApiOperation("查询工作人员比较")
    @GetMapping("/staff/compare")
    public ResponseResult staffCompare(){
        return staffService.compare();
    }

    @ApiOperation("价钱统计")
    @GetMapping("/thali/money")
    public ResponseResult thaliMoney(){
        return thaliService.thaliMoney();
    }

    @ApiOperation("单量")
    @GetMapping("/thali/count")
    public ResponseResult thaliCount(){
        return thaliService.thaliCount();
    }
}
